<?php
/**
 * UserModel.class.php
 * 院长用户模型
 * 
 * @author  王中艺 <wangzy_smile@qq.com>
 * @date    2017-08-27
 */

namespace Attach\Model;

class UserModel extends \Common\Model\UserModel {

    /**
     * 获取用户列表
     * @param   $userInfo     编外人员信息 id + role_id
     * @param   $condition
     * @param   $sortInfo 排序信息
     * @param   $page
     * @param   &$pageHtml
     * @param   $count 
     * @return  $list
     */
    function getList($userInfo, $condition, $sortInfo, $page, &$pageHtml, &$count){
        $where  = $this->setCondition($condition, 'a');
        if($userInfo['role_id'] == 101){
            $where['b.dean_id'] = $userInfo['id'];
        }else if(in_array($userInfo['role_id'], [102, 103, 104])){
            $where['b.expand_id']   = $userInfo['id'];
        }else{
            throw new \Exception('用户角色错误');
        }

        $query  = $this->table('t_user a')
            ->join('right join t_user_relative b on a.id = b.user_id')
            ->where($where)
            ->cache($this->cache);

        if($count === true){
            $count  = (clone $query)->count();
        }
        
        if($pageHtml === true){
            $Page   = new \Think\Page((clone $query)->count(), C('PAGE_COUNT'));
            $pageHtml   = $Page->angularShow();
        }

        //page为false，不执行查询
        if($page === false){
            return null;
        }

        //排序处理
        if($sortInfo){
            //中文排序处理
            if(in_array($sortInfo['field'], ['child_school', 'child_course_name'])){
                $sortInfo['field']   = sprintf('convert(a.`%s` using gb2312)', $sortInfo['field']);
            }
            $sort   = $sortInfo['desc'] ? $sortInfo['field'].' desc' : $sortInfo['field'];
        }else{
            $sort   = 'a.id desc';
        }

        //分页处理
        if(is_array($page)){
            $query->limit($page['start'], $page['length']);
        }else{
            $query->page($page, C('PAGE_COUNT'));
        }

        $list   = $query->order($sort)->field('a.*')->select();
        return parent::resolveColumn($list, true);
    }
}